#include"comm.hpp"

extern Log log;
int main()
{
    int shmid = GetShareMem();
    // log(Debug,"Creat success...");
    char* shmaddr = (char*)shmat(shmid,nullptr,0);//返回空间首地址
    // log(Debug,"attach success...,shmaddr:0x%x",shmaddr);

    Init init;

    //管道- 引入同步机制
    int fd = open(FIFO_FILE,O_RDONLY);
    if(fd < 0)
    { 
        log(Fatal,"open fail...");
        exit(FIFO_OPEN_ERR);
    }


    struct shmid_ds shmds;
    //ipc code: 通信
    while(true)
    {
        char c;
        ssize_t s = read(fd,&c,1);//对方不输入，就在这等，不读取
        if(s == 0) break;
        else if(s <0) break;

        cout<<"client say@ "<<shmaddr <<endl;

        // sleep(1);
        // shmctl(shmid,IPC_STAT,&shmds);//从shmid_ds结构体中获取IPC_STAT属性信息
        // cout<<"shmid size:"<<shmds.shm_segsz<<endl;//大小
        // cout<<"shm nattch:"<<shmds.shm_nattch<<endl;//连接状态
        //...
    }

    shmdt(shmaddr);
    // log(Debug,"detach success...,shmaddr:0x%x",shmaddr);
    shmctl(shmid,IPC_RMID,nullptr);
    // log(Debug,"IPC_RMID success...");

    close(fd);

    return 0;
}